#include <stdio.h>
#include <stdlib.h>

#define LENGTH 10

int data[LENGTH];

void init_array(int *data, int length)
{
	int i;
	for(i = 0; i < length; i++) {
		data[i] = rand()%100;
	}
}

void print_arrayj(int *data, int length) 
{
	int i;
	for(i = 0; i < length; i++) {
		printf("%-2d ", data[i]);
	}
	printf("\n");
}

void print_arrayx(int *data, int length)
{
	int i;
	for(i = 0; i < length; i++) {
		printf("data[%d] = %d\n", i, data[i]);
	}
}

//冒泡排序
void bubble(int *data, int length)
{
	int i,j,temp;
	for(i = length-1; i > 0; i--) {
		for(j = 0; j < i; j++) {
			if(data[j] > data[j+1]) {
				temp = data[j];
				data[j] = data[j+1];
				data[j+1] = temp;
			}
		}
	}
}

//选择排序
void selection_sort(int *data, int length)
{
	int i,j,temp,min = 0;
	for(i = length; i > 0; i--) {
		for(j = min + 1; j < length; j++) {
			if(data[min] > data[j]) {
				min = j;
			}
		}
		temp = data[min];
		data[min] = data[length - i];
		data[length - i] = temp;
		min = length - i + 1;
	}
}

int main()
{
	init_array(data, LENGTH);
	printf("before sort:\n");
	print_arrayj(data, LENGTH);
	selection_sort(data, LENGTH);
	printf("after sort:\n");
	print_arrayj(data, LENGTH);
	return 0;	
}
